package com.daigoudao.web.service.impl;

import java.util.List;

import com.daigoudao.web.dao.ICanBuyDao;
import com.daigoudao.web.dao.ITransInfoDao;
import com.daigoudao.web.model.CanBuyModel;
import com.daigoudao.web.model.PurchaseRecord;
import com.daigoudao.web.service.ICanBuyService;

public class CanBuyServiceImpl implements ICanBuyService {

	private ICanBuyDao canBuyDao;

	private ITransInfoDao transInfoDao;

	public void setTransInfoDao(ITransInfoDao transInfoDao) {
		this.transInfoDao = transInfoDao;
	}

	public void setCanBuyDao(ICanBuyDao canBuyDao) {
		this.canBuyDao = canBuyDao;
	}

	@Override
	public void addCanBuy(CanBuyModel canBuyModel) throws Exception {
		canBuyDao.addCanBuy(canBuyModel);
		if (canBuyModel.getImages() != null
				&& canBuyModel.getImages().size() > 0) {
			canBuyDao.insertImages(canBuyModel);
		}
	}

	@Override
	public void delCanBuy(int canId) throws Exception {
		canBuyDao.delCanBuy(canId);
	}

	@Override
	public void updateCanBuy(CanBuyModel canBuyModel) throws Exception {
		canBuyDao.updateCanBuy(canBuyModel);
		if (canBuyModel.getImages() != null
				&& canBuyModel.getImages().size() > 0) {
			canBuyDao.deleteImages(canBuyModel.getCanId());
			canBuyDao.insertImages(canBuyModel);
		}
	}

	@Override
	public int getCanBuyCount(CanBuyModel canBuyModel) throws Exception {
		return canBuyDao.getCanBuyCount(canBuyModel);
	}

	@Override
	public List<CanBuyModel> queryCanBuys(CanBuyModel canBuyModel)
			throws Exception {
		List<CanBuyModel> products = canBuyDao.queryCanBuys(canBuyModel);
		for (CanBuyModel product : products) {
			PurchaseRecord record = new PurchaseRecord();
			record.setProductId(product.getCanId());
			int transNum = transInfoDao.getPurhcaseRecordCount(record);
			product.setTransNum(transNum);
		}
		return products;
	}

	/**
	 * 修改代购信息状态
	 * 
	 * @param canBuyInfo
	 * @throws Exception
	 */
	public void updateCanBuyStatus(CanBuyModel canBuyModel) throws Exception {
		canBuyDao.updateCanBuyStatus(canBuyModel);
	}

	@Override
	public List<CanBuyModel> getHotCanBuyList() throws Exception {

		List<CanBuyModel> products = canBuyDao.getHotCanBuyList();
		for (CanBuyModel product : products) {
			PurchaseRecord record = new PurchaseRecord();
			record.setProductId(product.getCanId());
			int transNum = transInfoDao.getPurhcaseRecordCount(record);
			product.setTransNum(transNum);
		}
		return products;
	}

	@Override
	public CanBuyModel queryCanBuyDetail(int canId) throws Exception {
		CanBuyModel canBuyModel = canBuyDao.queryCanBuyDetail(canId);
		if (canBuyModel != null) {
			// 查询交易记录数
			PurchaseRecord record = new PurchaseRecord();
			record.setProductId(canId);
			int transNum = transInfoDao.getPurhcaseRecordCount(record);
			canBuyModel.setTransNum(transNum);
		}
		return canBuyModel;
	}

	public List<CanBuyModel> queryOtherCanBuys(CanBuyModel canBuyModel)
			throws Exception {
		return canBuyDao.queryOtherCanBuys(canBuyModel);
	}
}
